#include "cardpanel.h"

#include <QMouseEvent>
#include <QPainter>

CardPanel::CardPanel(QWidget *parent) : QWidget(parent)
{
}

// 设置获取图片函数
void CardPanel::setImage(const QPixmap &front, const QPixmap &back)
{
    m_front = front;
    m_back = back;

    setFixedSize(m_front.size());

    update(); // 更新，自动调用paintEvent()函数
}

// 获取卡牌正面函数
QPixmap CardPanel::getImage()
{
    return m_front;
}

// 扑克牌显示哪一面
void CardPanel::setFrontSide(bool flag)
{
    m_isfront = flag;
}

// 当前卡牌面状态
bool CardPanel::isFrontSide()
{
    return m_isfront;
}

// 窗口是否被选中
void CardPanel::setSeclected(bool flag)
{
    m_isSelect = flag;
}

// 当前窗口状态
bool CardPanel::isSelected()
{
    return m_isSelect;
}

// 扑克牌的花色以及点数
void CardPanel::setCard(const Card &card)
{
    m_card = card;
}

Card CardPanel::getCard()
{
    return m_card;
}

// 扑克牌的所有者
void CardPanel::setOwner(Player *player)
{
    m_owner = player;
}

Player *CardPanel::getOwner()
{
    return m_owner;
}

// 模拟扑克牌的点击事件
void CardPanel::clicked()
{
    emit cardSelected(Qt::LeftButton);
}

void CardPanel::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event)
    QPainter p(this);
    if(m_isfront)
    {
        p.drawPixmap(rect(), m_front);
    }
    else
    {
        p.drawPixmap(rect(), m_back);
    }
}

void CardPanel::mousePressEvent(QMouseEvent *event)
{
    emit cardSelected(event->button());
}
